package cn.yyh.serviceOrg.service.impl;

import cn.yyh.commons.constant.Constants;
import cn.yyh.commons.domain.LoginInfo;
import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.service.impl.MyBaseServiceImpl;
import cn.yyh.commons.utils.MD5Util;
import cn.yyh.commons.utils.StrUtils;
import cn.yyh.serviceOrg.entity.User;
import cn.yyh.serviceOrg.entity.dto.UserDTO;
import cn.yyh.serviceOrg.mapper.LoginInfoMapper;
import cn.yyh.serviceOrg.mapper.UserMapper;
import cn.yyh.serviceOrg.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: UserServiceImpl
 * @Package: cn.yyh.user.service.impl
 * @date 2021/6/30
 */
@Service
public class UserServiceImpl extends MyBaseServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void phoneRegister(UserDTO userDTO) {
        // 判断前端的数据是否为空
        if (ObjectUtils.isEmpty(userDTO.getPhone())
                || ObjectUtils.isEmpty(userDTO.getPassword())
                || ObjectUtils.isEmpty(userDTO.getConfirmPassword())
                || ObjectUtils.isEmpty(userDTO.getVerifyCode())) {
            throw new MyException("账号密码错误,请重新输入!");
        }
        // 判断前后两次密码是否一致
        if (!userDTO.getPassword().equals(userDTO.getConfirmPassword())) {
            throw new MyException("两次密码不一致,请重新输入!");
        }
        // 用当前电话去数据库查,如果不为null则已注册
        User userLogin = userMapper.getByPhone(userDTO.getPhone());
        if (userLogin != null) {
            throw new MyException("手机已注册,请直接登陆!");
        }
        // 用验证码去redis查,如果为null,表示为获取或者已经过期
        String temp = redisTemplate.opsForValue().get(Constants.BUSINESS_REGISTER_PREFIX + userDTO.getPhone());
        if (temp == null) {
            throw new MyException("验证码已过期,请重新获取验证码!");
        }
        // 截取出真正的验证码,和前端数据做对比
        String verifyCode = temp.split(":")[1];
        if (!userDTO.getVerifyCode().equals(verifyCode)) {
            throw new MyException("验证码输入错误!");
        }

        User user = new User();
        user.setUsername(userDTO.getPhone());
        user.setPhone(userDTO.getPhone());
        user.setPassword(userDTO.getPassword());
        save(user);
    }

    @Override
    public User getByLoginInfoId(Long id) {

        return baseMapper.getByLoginInfoId(id);
    }

    @Override
    public void save(User user) {
        initUser(user);
        LoginInfo loginInfo = user2LoginInfo(user);
        loginInfoMapper.save(loginInfo);

        user.setLoginInfoId(loginInfo.getId());
        super.save(user);
    }

    @Override
    public void update(User user) {
        initUser(user);
        LoginInfo loginInfo = user2LoginInfo(user);
        loginInfoMapper.update(loginInfo);

        super.update(user);
    }

    @Override
    public void delete(Long id) {

        if (!ObjectUtils.isEmpty(id)) {
            super.delete(id);
            User byId = userMapper.getById(id);
            if (byId != null) {
                loginInfoMapper.delete(byId.getLoginInfoId());
            }
        }

    }

    private void initUser(User user) {
        // 生成随机32位的盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 把密码加密
        String password = MD5Util.encrypByMd5(salt + user.getPassword());
        // 设置盐值和加密后的密码
        user.setSalt(salt);
        user.setPassword(password);
    }

    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user, loginInfo);
        loginInfo.setType(1);
        return loginInfo;
    }

}
